import 'package:flutter/material.dart';
import 'package:flutter_enjoy_plus/pages/home/componments/home_list.dart';
import 'package:flutter_enjoy_plus/pages/home/componments/home_nav.dart';
import 'package:flutter_enjoy_plus/utils/http.dart';
import 'package:flutter_enjoy_plus/utils/logger.dart';
import 'package:flutter_enjoy_plus/utils/toast.dart';
import 'package:provider/provider.dart';

import '../../stores/counter.dart';
// import 'package:fluttertoast/fluttertoast.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  void initState() {
    // 获取社区公告
    getNotifyList();
    super.initState();
  }

  List notifyList = [];

  getNotifyList() async {
    try {
      final res = await http.get('/announcement');
      // logger.i(res);
      if (res['code'] != 10000) return toast.toastFail('社区列表加载失败');
      toast.toastSuccess('社区列表加载成功');
      setState(() {
        notifyList = res['data'];
      });
    } catch (e) {
      // print('请求出现问题$e');
      logger.w('请求出现问题$e');
      toast.toastFail('社区列表加载失败');
    }
  }

  @override
  Widget build(BuildContext context) {
    // 获取 Counter 模型
    final counter = Provider.of<Counter>(context);
    return Scaffold(
        // backgroundColor: const Color.fromARGB(40, 85, 145, 175),
        backgroundColor: const Color.fromRGBO(228, 231, 242, 1),
        appBar: AppBar(
          title: Text('享+社区${counter.count}'),
          backgroundColor: Colors.transparent,
          centerTitle: true,
        ),
        body: ListView(
          scrollDirection: Axis.vertical,
          padding: const EdgeInsets.all(10),
          children: [
            // 导航条
            HomeNav(),
            // 中间广告图
            ClipRRect(
              borderRadius: BorderRadius.circular(10.0),
              child: Image.asset('assets/images/banner@2x.jpg'),
            ),
            // 社区公告
            HomeList(notifyList: notifyList)
          ],
        ));
  }
}
